import 'dart:math';

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(home: HomePage()));

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List _list = List.generate(Random().nextInt(20) + 10, (index) => 'item $index');

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("pull to refresh listview"),
      ),
      body: Center(
        child: ReorderableListView(
          onReorder: _onReorder,
          children: _list
              .map((e) => ListTile(
                    key: ObjectKey(e),
                    title: Text("aaa$e"),
                  ))
              .toList(),
        ),
      ),
    );
  }

  void _onReorder(int oldIndex, int newIndex) {
    setState(() {
      if(newIndex == 0) {
        newIndex = 1;
      }
      var item = _list.removeAt(oldIndex);
      _list.insert(newIndex-1, item);
    });
  }
}
